from info import redis_store
from info.utils.captcha.captcha import captcha
from . import passport_blue
from flask import request,make_response

# 请求路径: /passport/image_code
# 请求方式: GET
# 请求参数: cur_id, pre_id
# 返回值: 图片验证码
@passport_blue.route('/image_code')
def image_code():
    # - 1, 获取参数,cur_id
    cur_id =  request.args.get("cur_id")
    pre_id =  request.args.get("pre_id")

    # - 2,校验校验参数
    if not cur_id:
        return "参数不全"

    # 2,1 判断上一个图片验证码编号是否存在,存在则删除
    if pre_id:
        redis_store.delete("image_code:%s" % pre_id)

    # - 3,生成图片验证码
    name, text, image_code = captcha.generate_captcha()

    # - 4,存储到redis中
    redis_store.set("image_code:%s"%cur_id,text,300)

    # - 5,返回图片验证码
    response = make_response(image_code)
    response.headers["Content-Type"] = "image/png"
    return response
